package top.fengyulou.code.service;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import top.fengyulou.code.dao.ApiDao;
import top.fengyulou.code.entity.ApiForm;
import top.fengyulou.code.entity.Param;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 接口
 */
@Service
public class ApiService {

    @Resource
    private ApiDao apiDao;


    /**
     * 查询全部
     *
     * @return
     */
    public List<ApiForm> getAll() {
        return getList(null);
    }

    /**
     * 根据条件查询
     *
     * @param form
     * @return
     */
    public List<ApiForm> getList(ApiForm form) {
        List<ApiForm> list = apiDao.getList(form);
        if (ObjectUtils.isNotEmpty(list)) {
            for (ApiForm d : list) {
                //入参
                if (StringUtils.isNotBlank(d.getEnterParam())) {
                    List<Param> enterParamList = new ArrayList<>();
                    String[] split = d.getEnterParam().split("\n");
                    for (String s : split) {
                        String[] split1 = s.split("#");
                        Param param = new Param();
                        if (split1.length>0) {
                            param.setName(split1[0]);
                        }
                        if (split1.length>1) {
                            param.setType(split1[1]);
                        }
                        if (split1.length>2) {
                            param.setDesc(split1[2]);
                        }
                        enterParamList.add(param);
                    }
                    d.setEnterParamList(enterParamList);
                }
                //出参
                if (StringUtils.isNotBlank(d.getOutParam())) {
                    List<Param> outParamList = new ArrayList<>();
                    String[] split = d.getOutParam().split("\n");
                    for (String s : split) {
                        String[] split1 = s.split("#");
                        Param param = new Param();
                        if (split1.length>0) {
                            param.setName(split1[0]);
                        }
                        if (split1.length>1) {
                            param.setType(split1[1]);
                        }
                        if (split1.length>2) {
                            param.setDesc(split1[2]);
                        }
                        outParamList.add(param);
                    }
                    d.setOutParamList(outParamList);
                }
                //备注
                if (StringUtils.isNotBlank(d.getRemarks())) {
                    List<String> remarksList = new ArrayList<>();
                    String[] split = d.getRemarks().split("\n");
                    for (String s : split) {
                        remarksList.add(s);
                    }
                    d.setRemarksList(remarksList);
                }
            }
        }
        return list;
    }

    /**
     * 添加
     */
    public void add(List<ApiForm> list) {
        apiDao.add(list);
    }

    /**
     * 添加
     */
    public void add(ApiForm form) {
        List<ApiForm> list = new ArrayList<>();
        list.add(form);
        add(list);
    }

    /**
     * 修改
     */
    public void update(ApiForm form) {
        apiDao.update(form);
    }

    /**
     * 删除
     */
    public void delete(Long[] ids, Long userId) {
        apiDao.delete(ids, userId);
    }

    /**
     * 删除
     */
    public void delete(Long id, Long userId) {
        Long[] ids = new Long[]{id};
        delete(ids, userId);
    }

    /**
     * 修改接口状态
     * @param v2ApiForm
     */
    public void updateApiStatusById(ApiForm v2ApiForm) {
        apiDao.updateApiStatusById(v2ApiForm);
    }

    public long getCountByUrlAndProjectId(ApiForm form) {
        return apiDao.getCountByUrlAndProjectId(form);
    }
}
